﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Business.Base.Utils

{
    public class SerializationUtils
    {
        public static Object Read(String file)
        {
            if (!File.Exists(file))
            {
                return null;
            }
            FileStream fs = null;
            try
            {
                fs= new FileStream(file, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
              
                return bf.Deserialize(fs);
            }
            catch(Exception ex)
            {
                ex.ToString();
                return null;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        public static void Write(String file,object obj)
        {
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            try
            {
                FileStream fs = new FileStream(file, FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(fs, obj);
                fs.Close();
            }
            catch(Exception ex)
            {
                return ;
            }
        }
    }
}
